<?php


namespace app\api\controller\email;


error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING); //关闭警告
require_once "../extend/extend/Mail.php"; //引入邮箱配置
use Mail\Mail;  //使用邮箱配置中类

// 类名要大写
//require_once  "../extend/api/notice.php";
use think\App;

class Sendemail
{
    function index(){
        $json = [
            'code'=>200,
            'msg'=>"Access 这是进入邮箱模块",
        ];

        return json($json);

    }
   function email(){
       $json = [
           'code'=>200,
           'msg'=>"Access 这是邮箱发送模块",
       ];

//        return json($json);
        return  view('./index');
   }
    function send(){

        //接收前端数据
        $to = $_POST['email']!=""?$_POST['email']:"531556397@qq.com";
        $username = $_POST['username']!=""?$_POST['username']:"WT18-匿名用户";
        $range_code= $this ->random(4,1);
        $code = $range_code!=""? $range_code:"8750";
        //简单的表单验证

        if ($to !=""){
            $params = [
                "to"=>$to,
                "title"=>"WT18-JG-验证码",
                "data"=>['username'=>$username,'code'=>$code]
            ];
            $result = Mail::sendMail($params);
            if(!$result){
                $arr = ['code'=>100,"msg"=>"邮件发送失败~","data"=>"$to $username "];
            }else{
                $arr = ['code'=>200,"msg"=>"发送成功~","data"=>"","info"=>"请尽快登陆邮箱查询验证码完成注册！\n验证码有效时间为5分钟！"];
            }
        }else{
            $arr = ['code'=>0,"msg"=>"参数缺少~" ,"data"=>""];
        }

        $json = json_encode($arr,JSON_UNESCAPED_UNICODE);
        echo $json;
//        return $json;
        return ;


    }
    function random($length = 6 , $numeric = 0) {
        PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
        if($numeric) {
            $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        } else {
            $hash = '';
            $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
            $max = strlen($chars) - 1;
            for($i = 0; $i < $length; $i++) {
                $hash .= $chars[mt_rand(0, $max)];
            }
        }
        return $hash;
    }
}